package com.foreknow.dao.impl;

import com.foreknow.dao.BusinessDao;
import com.foreknow.db.DBManager;
import com.foreknow.db.JdbcTemplate;
import com.foreknow.mapping.EntityMapping;
import com.foreknow.mapping.MappingFactory;
import com.foreknow.model.Business;

import java.sql.SQLException;
import java.util.List;

/**
 * @author shkstart
 * @create 2021-08-05 15:50
 */
public class BusinessDaoImpl  extends BaseDAO implements BusinessDao {
    @Override
    public List<Object> getQueryAll() throws SQLException {
        String sql="select * from business";
        EntityMapping map = mappingFactory.getMap(MappingFactory.BUSINESS_MAPPNG);
        List<Object> query = jdbcTemplate.query(sql,map);

        return query;
    }

    @Override
    public boolean save(Business business) throws SQLException {
        String sql="insert into business (password,businessName,businessAddress,businessExplain,starPrice,deliverPrice) values(?,?,?,?,?,?) " ;
//        String sql1="insert into admin (userName,password) values(?,?)";
        int update = jdbcTemplate.update(sql, business.getPassword(), business.getBusinessName(), business.getBusinessAddress(), business.getBusinessExplain(), business.getStarPrice(), business.getDeliveryPrice());
        return update==1;
    }

    @Override
    public boolean delete(int id) throws SQLException {
        String sql="delete from business where businessId=?";
        int update = jdbcTemplate.update(sql, id);
        return update==1;
    }

    @Override
    public Object getQueryById(int id) throws SQLException {
        String sql="select * from business where businessId=?";
        EntityMapping map = mappingFactory.getMap(mappingFactory.BUSINESS_MAPPNG);
       List query= jdbcTemplate.query(sql,map,id);
       Object querybusiness=query.get(0);
        return querybusiness;
    }

    @Override
    public boolean Updatedao(Business business) throws SQLException {
        String sql="update business set password=?,businessName=?,businessAddress=?,businessExplain=?,starPrice=?,deliverPrice=? where businessId=? ";
        int update = jdbcTemplate.update(sql, business.getPassword(), business.getBusinessName(), business.getBusinessAddress(), business.getBusinessExplain(), business.getStarPrice(), business.getDeliveryPrice(),business.getBusinessId());
        return update==1;
    }

    @Override
    public List<Object> getNameAll(String name ) throws SQLException {
        String sql="select * from business where businessName like '%"+name+"%'";
        EntityMapping map = mappingFactory.getMap(MappingFactory.BUSINESS_MAPPNG);
        List<Object> query = jdbcTemplate.query(sql, map);
        return  query;
    }



}
